import { LinergradientServiceProvider } from './../../../providers/linergradient-service/linergradient-service';
import { ComSeriveProvider } from './../../../providers/com-serive/com-serive';
import { Component, ViewChild, Input, Renderer2 } from '@angular/core';
import { TeamSeriveProvider } from '../../../providers/team-serive/team-serive';
import { PcSeriveProvider } from '../../../providers/pc-serive/pc-serive';
import {
  IonicPage,
  NavController,
  NavParams,
  ToastController
} from 'ionic-angular';
import { BaseUI } from '../../../common/baseUI';
import { MySeriveProvider } from '../../../providers/my-serive/my-serive';

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage extends BaseUI {
  @ViewChild('input') input;

  view;
  phone = '';
  password: string = '';
  toastCount = 0;
  phones = '';

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public comSerive: ComSeriveProvider,
    public toastCtrl: ToastController,
    public lgService: LinergradientServiceProvider,
    public mySerive: MySeriveProvider,
    public render: Renderer2,
    public pcSeriveProvider: PcSeriveProvider,
    public teamSeriveProvider: TeamSeriveProvider
  ) {
    super();
  }

  ionViewDidLoad() {
    // setTimeout(() => {
    //   this.render.setAttribute(this.input._elementRef.nativeElement.children[0], "onkeypress", "return !/[eE]/.test(String.fromCharCode(event.keyCode))")
    // }, 100);
  }

  login() {
    const stringUser = JSON.stringify({
      phone: this.phone,
      password: this.password
    });
    const reg = /^1[34578][0-9]{9}$/;
    if (reg.test(this.phone)) {
      this.comSerive.userLogin(stringUser).subscribe(res => {
        if (res['status'] === 200) {
          localStorage.setItem('token', res['data']);
          this.mySerive.getToken(res['data']);
          this.teamSeriveProvider.setToken();
          this.pcSeriveProvider.setToken();
          this.navCtrl.setRoot('TabsPage');
        } else if (res['status'] === 400) {
          this.isShowToast(res['msg']);
          // console.log(res);
        }
      });
    } else if (!reg.test(this.phone)) {
      this.isShowToast('请输入正确的手机号');
    }
  }

  keyup() {
    const reg = /^1[34578][0-9]{9}$/;
    if (reg.test(this.phone)) {
      this.comSerive.checkPhoneInfo(this.phone).subscribe(data => {
        if (data['status'] === 200) {
          this.isShowToast('账号未注册');
        }
      });
    }
  }

  rep($event) { 
    //删除键
    if ($event.keyCode == 8 && this.phone.length == 0) {
      this.phone = this.phones = '';
      return;
    }
    //通过phone保存值的改变 来判断用户是否输入非法字符
    //输入非法字符后input的值会突然为空
    if (this.phone != '') {
      this.phones = this.phone;
    } else {
      this.phone = this.phones == '' ? '中文触发number' : this.phones;
    }
    this.phone = this.phone.replace(/[eE]/, '');
    if (this.phone.length > 11) this.phone = this.phone.slice(0, 11);
    this.keyup();
  }

  // 限制重复弹窗
  isShowToast(message) {
    if (this.toastCount === 0) {
      this.showToast(this.toastCtrl, message);
      this.toastCount++;
      setTimeout(() => {
        this.toastCount = 0;
      }, 3000);
    }
  }

  // 返回上一级
  pop() {
    this.navCtrl.pop();
  }

  gotoForgetPassword() {
    this.navCtrl.push('ForgetPasswordPage');
  }

  gotoRegister() {
    this.navCtrl.push('RegisterPage');
  }
}
